<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-htmlentitydefs.html" />
<link rel="prev" href="module-sgmllib.html" />
<link rel="parent" href="markup.html" />
<link rel="next" href="html-parser-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>8.3 htmllib -- A parser for HTML documents</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="8.2 sgmllib  "
  href="module-sgmllib.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="8. structured Markup Processing"
  href="markup.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="8.3.1 htmlparser Objects"
  href="html-parser-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-sgmllib.html">8.2 sgmllib  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">8. Structured Markup Processing</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="html-parser-objects.html">8.3.1 HTMLParser Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0010300000000000000000">
8.3 <tt class="module">htmllib</tt> --
         A parser for HTML documents</a>
</h1>

<p>
<a name="module-htmllib"></a>

<p>
<a id='l2h-1698' xml:id='l2h-1698'></a>

<p>
This module defines a class which can serve as a base for parsing text
files formatted in the HyperText Mark-up Language (HTML).  The class
is not directly concerned with I/O -- it must be provided with input
in string form via a method, and makes calls to methods of a
``formatter'' object in order to produce output.  The
<tt class="class">HTMLParser</tt> class is designed to be used as a base class for
other classes in order to add functionality, and allows most of its
methods to be extended or overridden.  In turn, this class is derived
from and extends the <tt class="class">SGMLParser</tt> class defined in module
<tt class="module"><a href="module-sgmllib.html">sgmllib</a></tt><a id='l2h-1699' xml:id='l2h-1699'></a>.  The <tt class="class">HTMLParser</tt>
implementation supports the HTML 2.0 language as described in
<a class="rfc" id='rfcref-99898' xml:id='rfcref-99898'
href="http://www.faqs.org/rfcs/rfc1866.html">RFC 1866</a>.  Two implementations of formatter objects are provided in
the <tt class="module"><a href="module-formatter.html">formatter</a></tt><a id='l2h-1700' xml:id='l2h-1700'></a> module; refer to the
documentation for that module for information on the formatter
interface.
<a id='l2h-1695' xml:id='l2h-1695'></a>
<p>
The following is a summary of the interface defined by
<tt class="class">sgmllib.SGMLParser</tt>:

<p>

<ul>
<li>The interface to feed data to an instance is through the <tt class="method">feed()</tt>
method, which takes a string argument.  This can be called with as
little or as much text at a time as desired; "<tt class="samp">p.feed(a);
p.feed(b)</tt>" has the same effect as "<tt class="samp">p.feed(a+b)</tt>".  When the data
contains complete HTML markup constructs, these are processed immediately;
incomplete constructs are saved in a buffer.  To force processing of all
unprocessed data, call the <tt class="method">close()</tt> method.

<p>
For example, to parse the entire contents of a file, use:
<div class="verbatim"><pre>
parser.feed(open('myfile.html').read())
parser.close()
</pre></div>

<p>
</li>
<li>The interface to define semantics for HTML tags is very simple: derive
a class and define methods called <tt class="method">start_<var>tag</var>()</tt>,
<tt class="method">end_<var>tag</var>()</tt>, or <tt class="method">do_<var>tag</var>()</tt>.  The parser will
call these at appropriate moments: <tt class="method">start_<var>tag</var></tt> or
<tt class="method">do_<var>tag</var>()</tt> is called when an opening tag of the form
<code>&lt;<var>tag</var> ...&gt;</code> is encountered; <tt class="method">end_<var>tag</var>()</tt> is called
when a closing tag of the form <code>&lt;<var>tag</var>&gt;</code> is encountered.  If
an opening tag requires a corresponding closing tag, like <code>&lt;H1&gt;</code>
... <code>&lt;/H1&gt;</code>, the class should define the <tt class="method">start_<var>tag</var>()</tt>
method; if a tag requires no closing tag, like <code>&lt;P&gt;</code>, the class
should define the <tt class="method">do_<var>tag</var>()</tt> method.

<p>
</li>
</ul>

<p>
The module defines a parser class and an exception:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1696' xml:id='l2h-1696' class="class">HTMLParser</tt></b>(</nobr></td>
  <td><var>formatter</var>)</td></tr></table></dt>
<dd>
This is the basic HTML parser class.  It supports all entity names
required by the XHTML 1.0 Recommendation (<a class="url" href="http://www.w3.org/TR/xhtml1">http://www.w3.org/TR/xhtml1</a>).  
It also defines handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
</dl>

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-1697' xml:id='l2h-1697' class="exception">HTMLParseError</tt></b></dt>
<dd>
Exception raised by the <tt class="class">HTMLParser</tt> class when it encounters an
error while parsing.

<span class="versionnote">New in version 2.4.</span>

</dd></dl>

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-formatter.html">formatter</a></tt>:</b>
    <dd>Interface definition for transforming an
                        abstract flow of formatting events into
                        specific output events on writer objects.
  </dl>
  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-HTMLParser.html">HTMLParser</a></tt>:</b>
    <dd>Alternate HTML parser that offers a slightly
                         lower-level view of the input, but is
                         designed to work with XHTML, and does not
                         implement some of the SGML syntax not used in
                         ``HTML as deployed'' and which isn't legal
                         for XHTML.
  </dl>
  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-htmlentitydefs.html">htmlentitydefs</a></tt>:</b>
    <dd>Definition of replacement text for XHTML 1.0 
                             entities.
  </dl>
  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-sgmllib.html">sgmllib</a></tt>:</b>
    <dd>Base class for <tt class="class">HTMLParser</tt>.
  </dl>
</div>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="html-parser-objects.html">8.3.1 HTMLParser Objects</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="8.2 sgmllib  "
  href="module-sgmllib.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="8. structured Markup Processing"
  href="markup.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="8.3.1 htmlparser Objects"
  href="html-parser-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-sgmllib.html">8.2 sgmllib  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">8. Structured Markup Processing</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="html-parser-objects.html">8.3.1 HTMLParser Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
